import { reactive } from "vue";
import { getById } from "@/api/health";

const uploadServerUrl = `${import.meta.env.VITE_API_SERVER}`;

export const _$ = reactive({
  requestParam: {
    id: null,
  },
  responseParam: {},
});

export const $ = {
  loadDetail() {
    getById(_$.requestParam.id).then((data) => {
      _$.responseParam = data;
    });
  },

  onDownload(fileUrl) {
    fetch(fileUrl).then((res) =>
      res.blob().then((blob) => {
        let a = document.createElement("a");
        let url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = "体检文件";
        a.click();
        window.URL.revokeObjectURL(url);
      })
    );
    // const downloadEle = document.createElement("a");
    // downloadEle.setAttribute(
    //   "href",
    //   `${uploadServerUrl}${file}?Authorization=Bearer ${localStorage.getItem(
    //     "ats-user-token"
    //   )}`
    // );
    // downloadEle.setAttribute("download", "download");
    // downloadEle.setAttribute("target", "_blank");
    // document.body.appendChild(downloadEle);
    // downloadEle.click();
    // document.body.removeChild(downloadEle);
  },

  clean() {
    _$.responseParam = {};
  },
};

export const to = {};
